
Imports cmplib01

Public Class frmEndGame

    Private ptrGameData As cmplib01.CampaignerGameData

    Public Shadows Sub showDialog(ByRef game As cmplib01.CampaignerGameData)

        ptrGameData = game
        If ptrGameData Is Nothing Then Exit Sub

        '// list the game end crapola
        If game.gameEndDetails.turnRunOut Then TurnRunOutEnd()
        If game.gameEndDetails.VictoryWin Then VictoryConditionEnd()

        '// if neither path is followed, this form isn't shown!!


    End Sub


    Private Sub TurnRunOutEnd()
        Try
            lblVictoryResult.Text = "The game has ended due to turn limit." & vbCrLf

            '// put in the victory condition standings
            lblVictoryResult.Text &= VCStanding()

            MyBase.ShowDialog()
        Catch ex As Exception
            HandleException("frmEndGame.TurnRunOutEnd", ex)
        End Try
    End Sub

    Private Sub VictoryConditionEnd()
        Try

            '// show the name of the victorious faction!
            Dim p1, p2 As Long
            Dim f1, f2 As Faction

            AssignFactions(f1, f2)

            VCStandingForFaction(f1, p1)
            VCStandingForFaction(f2, p2)

            If p1 > p2 Then
                lblVictoryResult.Text = f1.FactionName & " is victorious with " & p1 & " victory points."
            ElseIf p1 < p2 Then
                lblVictoryResult.Text = f2.FactionName & " is victorious with " & p2 & " victory points."
            ElseIf p1 = p2 Then
                '// a fricken TIE!!!
                lblVictoryResult.Text = "Both factions have completed their victory conditions! The campaign is a draw."
            End If

            MyBase.ShowDialog()
        Catch ex As Exception
            HandleException("frmEndGame.VictoryConditionEnd", ex)
        End Try
    End Sub

    Private Sub AssignFactions(ByRef f1 As Faction, ByRef f2 As Faction)
        Try
            '// get both factions.
            For Each fkey As String In ptrGameData.Faction_g.HT.Keys
                If Not f1 Is Nothing Then
                    f2 = ptrGameData.Faction_g.Item(fkey)
                Else
                    f1 = ptrGameData.Faction_g.Item(fkey)
                End If

            Next
        Catch ex As Exception
            HandleException("frmEndGame.AssignFactions", ex)
        End Try
    End Sub

    Private Function VCStanding() As String
        '// returns: Joe: 23, Bob: 33. 
        Dim st As String = "No data on victory condition standings."

        Try
            Dim f1 As cmplib01.Faction = New Faction
            Dim f2 As cmplib01.Faction = New Faction

            Dim f1points, f2points As Long

            AssignFactions(f1, f2)

            st = VCStandingForFaction(f1, f1points) & ", " & VCStandingForFaction(f2, f2points)
            Return st

        Catch ex As Exception
            st = "Invalid Data Found"
        End Try

        Return st

    End Function

    Private Function VCStandingForFaction(ByRef fact As cmplib01.Faction, ByRef points As Long) As String
        Try
            For Each vc As IVictoryCondition In fact.FactionVictoryConditions
                If vc.Achieved Then points += vc.VPValue
            Next

            Return fact.FactionName & ": " & points

        Catch ex As Exception
            HandleException("frmEndGame.VCStandingForFaction", ex)
            Return "<Invalid Data>"
        End Try
    End Function

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.Hide()
    End Sub

    Private Sub chkIgnore_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkIgnore.CheckedChanged
        If ptrGameData Is Nothing Then Exit Sub

        Try
            ptrGameData.selectedPlayer.IgnoreEndGame = True
        Catch ex As Exception
            HandleException("frmEndGame.chkIgnore", ex)
        End Try

    End Sub
End Class